package com.zhang.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhang.entity.ShoppingCategory2;
import com.zhang.mapper.ShoppingCategory2Mapper;
import com.zhang.service.ShoppingCategory2Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ZhangD
 * @since 2021-07-13
 */
@Service
public class ShoppingCategory2ServiceImpl extends ServiceImpl<ShoppingCategory2Mapper, ShoppingCategory2> implements ShoppingCategory2Service {


    @Autowired
    private ShoppingCategory2Mapper shoppingCategory2Mapper;
    @Override
    public Page<ShoppingCategory2> selectShoppingCategory2(int category1Id, int pageNum, int pageSize) {
        Page<ShoppingCategory2> page = new Page<>(pageNum, pageSize);
        QueryWrapper<ShoppingCategory2> wrapper = new QueryWrapper<>();
        wrapper.in("category1_id",category1Id);
        return shoppingCategory2Mapper.selectPage(page, wrapper);
    }

    @Override
    public Page<ShoppingCategory2> category2Name(String category2Name, int pageNo, int pageSize) {
        Page<ShoppingCategory2> page = new Page<>(pageNo,pageSize);
        QueryWrapper<ShoppingCategory2> wrapper = new QueryWrapper<>();
        wrapper.like("category2_name",category2Name);
        return shoppingCategory2Mapper.selectPage(page,wrapper);
    }

    @Override
    public int changeValue(String category2Id) {
        ShoppingCategory2 category2 = new ShoppingCategory2();
        UpdateWrapper<ShoppingCategory2> wrapper = new UpdateWrapper<>();
        wrapper.eq("id",category2Id);
        if(shoppingCategory2Mapper.selectById(category2Id).getValue()) {
            category2.setValue(false);
        }else {
            category2.setValue(true);
        }
        return shoppingCategory2Mapper.update(category2,wrapper);
    }

    @Override
    public ShoppingCategory2 getCategory(String category2Id) {
        return shoppingCategory2Mapper.selectById(category2Id);
    }

    @Override
    public int addCategory2(String category2Name, String category2Record,String category1Id) {
        ShoppingCategory2 category2 = new ShoppingCategory2();
        category2.setCategory2Name(category2Name);
        category2.setCategory2Record(category2Record);
        category2.setCategory1Id(category1Id);
        return shoppingCategory2Mapper.insert(category2);
    }

    @Override
    public int updateCategory2(String id, String category2Name, String category2Record,String category1Id) {
        UpdateWrapper<ShoppingCategory2> wrapper = new UpdateWrapper<>();
        wrapper.eq("id",id);
        ShoppingCategory2 category2 = new ShoppingCategory2();
        if (category2Record!=null){
            category2.setCategory2Record(category2Record);
        }
        if (category2Name!=null) {
            category2.setCategory2Name(category2Name);
        }
        category2.setCategory1Id(category1Id);
        return shoppingCategory2Mapper.update(category2,wrapper);
    }
}
